<template>
  <el-dialog v-model="visible" title="添加成员">
    <el-table
      stripe
      border
      ref="tableRef"
      v-loading="loading"
      :data="tableData"
      key="id"
      
      @select="handleSelectionChange"
      @select-all="selectAll"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column prop="nickname" label="姓名" />
      <el-table-column prop="mobile" label="手机号码" />
      <el-table-column prop="status_desc" label="账号状态" />
    </el-table>

    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm"> 确定 </el-button>
        <el-button @click="onCancel">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import api from "@/api/auth.js";

const props = defineProps({
  typeName: { type: String, default: "新增" },
});
const visible = ref(false);
const tableData = ref([]);
const tableRef = ref();
const total = ref(0);
const selectUsers = ref([]); // 当前页选择的数据
const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});
const loading = ref(false);
const emits = defineEmits(["success"]);
const selectAll = (arr) => {
  // 是否全选
  if (arr.length !== 0) {
    // 全选
    arr.forEach((item) => {
      const bool = selectUsers.value.some((user) => user.id === item.id); // 存在返回true 否则返回false
      if (!bool) {
        // 不存在添加
        selectUsers.value.push(item);
      }
    });
  } else {
    // 取消全选
    tableData.value.forEach((item) => {
      selectUsers.value = selectUsers.value.filter(
        (user) => user.id !== item.id
      );
    });
  }
};
const handleSelectionChange = (arr, row) => {
  // 选择
  const bool = selectUsers.value.some((user) => user.id === row.id); // 存在返回true 否则返回false
  if (bool) {
    // 存在删除
    selectUsers.value = selectUsers.value.filter((user) => user.id !== row.id);
  } else {
    // 添加
    selectUsers.value.push(row);
  }
};
const echoSelected = () => {
  setTimeout(() => {
    // 回显选中
    if (selectUsers.value.length > 0) {
      tableData.value.forEach((item) => {
        if (selectUsers.value.some((ele) => ele.id === item.id)) {
          tableRef.value.toggleRowSelection(item);
        }
      });
    }
  }, 500);
};

const initData = async () => {
  loading.value = true;
  try {
    const res = await api.listNoRoleMenu(queryData.value);
    tableData.value = res.data.list;
    total.value = res.data.total;
    echoSelected();
  } finally {
    loading.value = false;
  }
};

const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData();
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData();
};

const submitForm = () => {
  visible.value = false;
  emits("success", selectUsers.value);
};

const onCancel = () => {
  visible.value = false;
};
const openDialog = (roleid) => {
  visible.value = true;
  queryData.value.roleid = roleid || "";
  tableData.value = [];
  selectUsers.value = [];
  handleCurrentChange(1);
};

defineExpose({
  openDialog,
});
</script>
